package com.geostar.constructionland.analyse.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.geostar.constructionland.analyse.service.BaseAdjustVersionService;
import com.geostar.constructionland.common.entity.response.AdjustAgrVersion;
import com.geostar.constructionland.common.entity.response.ExtJsAjaxResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 平差版本分析控制器
 *
 * @author zhu jiacheng
 * @date 2020/7/22 10:55
 */
@RestController
@Api("平差版本分析控制器")
public class AdjustVersionController {

    @Autowired
    private BaseAdjustVersionService adjustVersionService;

    @RequestMapping(path = "/adjustVersion/add", method = {RequestMethod.POST})
    @ApiOperation("新增")
    public ExtJsAjaxResponse add(@RequestBody @Valid AdjustAgrVersion adjustAgrVersion) {
        adjustAgrVersion.setModifiedTime(new Date());
        return ExtJsAjaxResponse.createBySuccess(adjustVersionService.save(adjustAgrVersion));
    }


    @RequestMapping(path = "/adjustVersion/update", method = {RequestMethod.POST})
    @ApiOperation("修改")
    public ExtJsAjaxResponse update(@RequestBody @Valid AdjustAgrVersion adjustAgrVersion) {
        adjustAgrVersion.setModifiedTime(new Date());
        return ExtJsAjaxResponse.createBySuccess(adjustVersionService.updateById(adjustAgrVersion));
    }

    @RequestMapping(path = "/adjustVersion/delete", method = {RequestMethod.POST})
    @ApiOperation("删除")
    public ExtJsAjaxResponse delete(@NotBlank(message = "ID不能为空") @RequestParam("id") String id) {
        adjustVersionService.deleteById(id);
        return ExtJsAjaxResponse.createBySuccessMessage("删除平差版本信息成功!");
    }


    @RequestMapping(path = "/adjustVersion/list", method = {RequestMethod.POST})
    @ApiOperation("分页查询")
    public ExtJsAjaxResponse list(
            @NotNull(message = "页码不能为空") @RequestParam(value = "page") int pageNo,
            @NotNull(message = "每页数量不能为空") @RequestParam(value = "limit") int pageNum,
            @RequestParam(required = false) String query_param_adjust_version_name) {
        IPage<AdjustAgrVersion> adjustAgrVersionIPage
                = adjustVersionService.getAdjustVersionForPage(pageNo, pageNum, query_param_adjust_version_name);
        return ExtJsAjaxResponse.createBySuccess(adjustAgrVersionIPage);
    }
}
